#include <stdlib.h>
#include <string>
#include <iostream>
#include <memory>
#include <rise/common/exmacros.h>
#include <rise/common/Log.h>
#include <rise/common/Exception.h>
#include <rise/common/DataEncoder.h>
#include <rise/common/StreamBuffer.h>
#include <staff/common/Value.h>
#include <staff/common/QName.h>
#include <staff/common/Attribute.h>
#include <staff/common/DataObject.h>
#include <staff-mq/client/Publisher.h>


int main(int nArgs, char* szArgs[])
{
  std::string sServerHost = "127.0.0.1";
  unsigned short ushServerPort = 8768;
  unsigned long ulDataSize = 1024 * 1024 * 10; // 10mb by default

  for (int nArg = 1; nArg < nArgs; ++nArg)
  {
    if(szArgs[nArg])
    {
      const std::string& sArg = std::string(szArgs[nArg]);
      if (sArg.substr(0, 2) == "-s")
      {
        sServerHost = sArg.substr(2);
      }
      else
      if (sArg.substr(0, 2) == "-p")
      {
        ushServerPort = static_cast<unsigned short>(atoi(sArg.substr(2).c_str()));
      }
      else
      if (sArg.substr(0, 2) == "-c")
      {
        ulDataSize = static_cast<unsigned long>(atol(sArg.substr(2).c_str()));
      }
      else
      if (sArg.substr(0, 2) == "-h")
      {
        std::cerr << "Publisher binary data transfer test.\n\n"
            << szArgs[0] << "[ -h][ -s<Server host>][ -p<port>][ -c<bytes count>]\n\n";
        return 0;
      }
      else
      {
        std::cerr << "unknown arg: " << sArg << "\n";
        return 1;
      }
    }
  }


  try
  {
    smq::Publisher tPublisher;
    tPublisher.Init("publisher-queueing", sServerHost, ushServerPort);

    tPublisher.CreateQueue("queue-queueing");

    // first message
    {
      staff::CDataObject tdoMsg("msg1");
      tdoMsg.SetText("first message");

      // publishing into queue
      std::cout << "Publishing message 1 into \"queue-queueing\"" << std::endl;
      tPublisher.Publish("queue-queueing", tdoMsg);
    }

    // second message
    {
      staff::CDataObject tdoMsg("msg2");
      tdoMsg.SetText("second message");

      // publishing into queue
      std::cout << "Publishing message 2 into \"queue-queueing\"" << std::endl;
      tPublisher.Publish("queue-queueing", tdoMsg);
    }

    // third message
    {
      staff::CDataObject tdoMsg("msg3");
      tdoMsg.SetText("third message");

      // publishing into queue
      std::cout << "Publishing message 3 into \"queue-queueing\"" << std::endl;
      tPublisher.Publish("queue-queueing", tdoMsg);
    }

//    tPublisher.RemoveQueue("queue-queueing");
  }
  RISE_CATCH_ALL

  return 0;
}
